<?php
include './config/config.php';

if ($_POST) {

  // todo 判断是否存在相同岗位
  if ($_POST['action'] == 'check') {
    $name = $_POST['val'];

    $sql = "SELECT * FROM `pre_department` WHERE `name` = '$name'";

    $data = query($sql);

    if ($data) {
      error("部门已存在");
    } else {
      success("部门可以使用");
    }
  }

  $formData = array(
    "name" => $_POST['name']
  );

  // $sql = "SELECT * FROM `pre_department` ORDER BY `id` ASC";
  // $dataList = queryAll($sql);
  // foreach ($dataList as $item) {
  //   if ($formData['name'] == $item['name']) {
  //     error("部门已存在,请勿重复添加");
  //     exit;
  //   }
  // }

  $res = insertInfo("department", $formData);

  if ($res) {
    success("部门添加成功");
  } else {
    error('部门添加失败');
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
</head>

<body>
  <!--<![endif]-->

  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加部门</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="departmentList.php">DepartmentList</a> <span class="divider">/</span></li>
      <li class="active">DepartmentAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='departmentList.php'"><i class="icon-list"></i> 部门列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <!-- action="personAddRouter.php" -->
              <form id="add_form" method="POST">
                <label for="DepartmentName">部门名</label>
                <input type="text" id="DepartmentName" placeholder="DepartmentName" name="name">
                <span id="username"></span>

                <br>
                <button class="btn btn-primary" type="submit" class="subBtn">提交</button>
              </form>
            </div>
          </div>
        </div>
        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>

  <script src="./assets/lib/jquery-1.8.1.min.js"></script>
  <script>
    $("#add_form").on("submit", function(e) {
      e.preventDefault()
      var formData = $(this).serialize()
      console.log(formData);
      $.ajax({
        type: "post",
        url: "departmentAdd.php",
        data: formData,
        dataType: "json",
        success: function(res) {
          if (res.code === 0) {
            alert(res.msg);
          } else if (res.code === 1) {
            alert(res.msg);
            location.href = 'departmentList.php';
          }
        },
        error: function(err) {
          console.log(err);
        }
      });
    });

    // todo 检测部门名
    $('input[name="name"]').change(function(e) {
      let val = $(this).val();
      $.ajax({
        type: "post",
        url: "departmentAdd.php",
        data: {
          val,
          action: 'check'
        },
        dataType: "json",
        success: function(res) {
          if (res.code === 1) {
            $("#username").html(res.msg).css({
              "color": "green"
            });
          } else if (res.code === 0) {
            $("#username").html(res.msg).css({
              "color": "red"
            });
          }
        },
        error: function(err) {
          console.log(err);
        }
      });
    })
  </script>
</body>

</html>
<?php include_once('common/script.php'); ?>